home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.mactech.com 2010
/
ftp.mactech.com.tar
/
ftp.mactech.com
/
src
/
mactech
/
volume14_1998
/
14.12.sit
/
14.12
/
Poor Man's Bryce 3.0
/
PictRead.c
< prev
next >
Wrap
Text File
|
1998-08-10
|
4KB
|
154 lines
// PictRead.c (for PMB v3.0)
//
// ©1998 by Kas Thomas. Portions ©1998 by Xplain Corp. and MacTech Magazine.
//
// These routines let our user find a PICT file to use as a texture map.
#include <StandardFile.h>
#include <Memory.h>
#include <TextUtils.h>
#include <QDOffscreen.h>
#include "QD3D.h"
#include "QD3DStorage.h"
#include "PictRead.h"
// ------------------------ OpenPICTFile() ------------------------
// open a PICT file and read it into a PICT handle
PicHandle OpenPICTFile(
short vRefNum,
Str255 fName)
{
short fRefNum = 0;
OSErr err;
long curEOF;
PicHandle myPic = nil;
long count;
err = FSOpen(fName, vRefNum, &fRefNum); // open PICT file
if (err) goto bail;
err = GetEOF(fRefNum, &curEOF); // get size of file
if (err) goto bail;
err = SetFPos(fRefNum, fsFromStart, 512L); // move past header
if (err) goto bail;
count = curEOF - 512L; // size of data to read
myPic = (PicHandle)NewHandleClear(count); // allocate PicHandle
if ( myPic == nil )
goto bail;
HLock((Handle)myPic); // lock the handle
err = FSRead(fRefNum, &count,(Ptr) *myPic); // read the PICT info
HUnlock((Handle)myPic); // unlock handle
bail:
IOErr( err ); // report any errors
if (fRefNum) // if file was open, close it
FSClose( fRefNum );
return (myPic); // return the PicHandle
}
// -------------------------- LoadMapPICT() ---------------------------
// This is where our PICT texture gets made into a StoragePixmap so
// QD3D can use it. Remember, QD3D is cross-platform and can't use
// regular PixMaps!
//
// Version 3.0: Makes all textures 16-bit instead of 32, for speed.
short LoadMapPICT(
PicHandle pict,
unsigned long mapSizeX,
unsigned long mapSizeY,
TQ3StoragePixmap *bMap)
{
unsigned long pictMapAddr;
Rect rectGW;
GWorldPtr pGWorld;
PixMapHandle hPixMap;
unsigned long pictRowBytes;
QDErr err;
GDHandle oldGD;
GWorldPtr oldGW;
short success = 1;
extern void TellUser(Str255 s, OSErr e);
GetGWorld(&oldGW, &oldGD); // save current port
SetRect(&rectGW, 0, 0, (unsigned short)mapSizeX, (unsigned short)mapSizeY);
// Important to preflight memory now, before asking for a GWorld.
if (TempFreeMem() < mapSizeX * mapSizeY * 4L + 1000) {
TellUser("\pNot enough memory to do this right now.", 0);
return 0;
}
// Now create a 32-bit GWorld.
err = NewGWorld(&pGWorld, 16, &rectGW, 0, 0, useTempMem);
if (err != noErr)
return 0;
// Get some basic info
hPixMap = GetGWorldPixMap(pGWorld);
pictMapAddr = (unsigned long)GetPixBaseAddr (hPixMap);
pictRowBytes = (unsigned long)(**hPixMap).rowBytes & 0x3fff;
// Get ready to draw offscreen
SetGWorld(pGWorld, nil);
LockPixels(hPixMap);
EraseRect(&rectGW);
// Draw it
DrawPicture(pict, &rectGW);
// Finally, convert it to a storage pixmap
bMap->image = Q3MemoryStorage_New((const unsigned char *)pictMapAddr,
pictRowBytes * mapSizeY);
if (bMap->image == nil) { // error
success = 0;
goto bail;
}
UnlockPixels(hPixMap);
bMap->width = mapSizeX; // fill out the rest of the fields
bMap->height = mapSizeY;
bMap->rowBytes = pictRowBytes;
bMap->pixelSize = 16;
bMap->pixelType = kQ3PixelTypeRGB16;
bMap->bitOrder = kQ3EndianBig;
bMap->byteOrder = kQ3EndianBig;
bail:
SetGWorld(oldGW, oldGD); // restore the world
DisposeGWorld(pGWorld); // kill the old world
return success;
}
// ------------------------------ IOErr() -------------------------------
// Put up a basic Alert stating the error code.
void IOErr( OSErr errcode )
{
Str63 msg;
if (!errcode) return;
NumToString( errcode, msg );
ParamText("\pAn I/O error occurred: ",msg,"\p","\p");
NoteAlert( IO_ALERT, 0 );
}